﻿using AutoMapper;
using MiniErp.Common.DB.MySqlDB;
using MiniErp.Entity.DataStreams.BOs;
using MiniErp.Entity.DataStreams.DOs;
using MiniErp.Entity.DataStreams.DTOs.Rights;
using MiniErp.Repository.BaseRepository;
using MiniErp.Repository.UnitOfWorkCore;
using MiniErp.Service.BusinessService.IService.Rights;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniErp.Service.BusinessService.Service.Rights
{
    public class RightsService : BaseService, IRightsService
    {
        private readonly BaseRepository<MysqlContext, JobsSystemsRights> _jsrr;
        public RightsService(IMysqlUnitOfWork mysqlUnitOfWork, IMapper mapper) : base(mysqlUnitOfWork, mapper)
        {
            _jsrr = _mysqlUnitOfWork.CreateRepository<MysqlContext, JobsSystemsRights>();
        }
        public async Task<bool> AddRightsAsync(AddRightsDTO rightsDTO)
        {
            var nums = await _jsrr.GetInfoNumsAsync(item => item.JobsId == rightsDTO.JobsId && item.SystemsId == rightsDTO.SystemsId);
            if (nums > 0) throw new ExceptionBO("该用户已拥有该系统权限");
            var rights = _mapper.Map<JobsSystemsRights>(rightsDTO);
            _jsrr.AddInfo(rights);
            return await _mysqlUnitOfWork.SaveAsync() == 1
                ? true
                : throw new ExceptionBO("新增权限失败");
        }

        public async Task<bool> DeleteRightsAsync(int JobsSystemsRightsId)
        {
            var result = await _jsrr.GetInfoAsync(item => item.JobsSystemsRightsId == JobsSystemsRightsId);
            if (result == null) throw new ExceptionBO("指定权限不存在");
            _jsrr.DeleteInfo(result);
            return await _mysqlUnitOfWork.SaveAsync() == 1
               ? true
               : throw new ExceptionBO("删除权限失败");
        }

        public async Task<List<RightsDTO>> GetAllRightsAsync(int page, int num)
        {
            var result = await _jsrr.GetInfoAsyncs<int>(page, num, "JobsSystemsRightsId");
            return result.Count != 0
                ? _mapper.Map<List<RightsDTO>>(result)
                : throw new ExceptionBO("未查询到权限数据");
        }

        public async Task<List<RightsDTO>> GetRightsByJobsIdAsync(int jobsid, int page, int num)
        {
            var result = await _jsrr.GetInfoAsyncs<int>(page, num, "JobsSystemsRightsId", item => item.JobsId == jobsid);
            return result.Count != 0
                ? _mapper.Map<List<RightsDTO>>(result)
                : throw new ExceptionBO("未查询到权限数据");
        }

        public async Task<List<RightsDTO>> GetRightsBySystemsIdAsync(int systemsid, int page, int num)
        {
            var result = await _jsrr.GetInfoAsyncs<int>(page, num, "JobsSystemsRightsId", item => item.SystemsId == systemsid);
            return result.Count != 0
                ? _mapper.Map<List<RightsDTO>>(result)
                : throw new ExceptionBO("未查询到权限数据");
        }

        public async Task<bool> UpdateRightsAsync(UpdateRightsDto rightsDTO)
        {

            var result = await _jsrr.GetInfoAsync(item => item.JobsSystemsRightsId == rightsDTO.JobsSystemsRightsId);
            if (result == null) throw new ExceptionBO("指定权限不存在");
            result.RightsId = rightsDTO.RightsId;
            _jsrr.UpdateInfo(result);
            return await _mysqlUnitOfWork.SaveAsync() == 1
                ? true
                : throw new ExceptionBO("修改权限失败");
        }
    }
}
